<template>
  <!--用户列表-->
  <div class="statistics-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button type="primary" @click="add()">添加大咖</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        sortable
        type="index"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="appid_info.nickname"
        label="用户名"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="appid_info.mobile"
        label="手机号"
      />
      <el-table-column align="center" show-overflow-tooltip label="头像">
        <template #default="{ row }">
          <el-image
            style="width: 60px; height: 60px"
            :src="row.appid_info.logo"
            :preview-src-list="[row.appid_info.logo]"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="order"
        label="排序"
      />
      <el-table-column align="center" label="操作">
        <template #default="{ row }">
          <el-button type="text" @click="edit(row)">编辑</el-button>
          <el-button type="text" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :title="wintit" :visible.sync="dialogFormVisible">
      <el-table
        v-loading="listLoading"
        :data="userlist"
        @selection-change="setSelectRows"
      >
        <el-table-column
          align="center"
          show-overflow-tooltip
          label="序号"
          sortable
          type="index"
        />
        <el-table-column
          align="center"
          show-overflow-tooltip
          prop="nickname"
          label="名称"
        />
        <el-table-column
          align="center"
          show-overflow-tooltip
          prop="mobile"
          label="手机号"
        />
        <el-table-column align="center" show-overflow-tooltip label="身份">
          <template #default="{ row }">
            {{ row.type == 1 ? '公司' : '个人' }}
          </template>
        </el-table-column>
        <el-table-column align="center" show-overflow-tooltip label="头像">
          <template #default="{ row }">
            <el-image
              style="width: 60px; height: 60px"
              :src="row.logo"
              :preview-src-list="[row.logo]"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="{ row }">
            <el-button
              v-if="row.user_master_is == 2"
              type="text"
              @click="send(row)"
            >
              添加成为大咖
            </el-button>
            <el-button
              v-if="row.user_master_is == 1"
              type="warning"
              @click="del(row)"
            >
              取消大咖身份
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="queryForm.page"
        :page-size="queryForm.list_row"
        :layout="layout"
        :total="totalOnetag"
        @size-change="winSizeChange"
        @current-change="winCurrentChange"
      />
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑排序" :visible.sync="dialogEdit">
      <el-input v-model="sort" clearable />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
  import {
    getUserLists,
    getAppMasterJoins,
    addAppMasterJoins,
    delAppMasterJoins,
    editAppMasterJoins,
  } from '@/api/common'

  export default {
    name: 'Super',
    data() {
      return {
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
        },
        listLoading: false,
        wintit: '',
        list: [
          {
            title: '名称',
            user: '张三',
            to: '李四',
            statu: 1,
            create_time: '2021-01-01 12:01',
            back_time: '2021-01-08 12:01',
          },
        ],
        labels: [],
        userlist: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        totalOnetag: '',
        dialogFormVisible: false,
        superid: '',
        sort: '',
        dialogEdit: false,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      handleDetail() {},
      //搜索
      search() {
        this.getData()
      },
      getData() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
        }
        getAppMasterJoins(params).then((data) => {
          this.list = data.data
          this.total = data.count
        })
      },
      getUser() {
        let params = {
          page: this.queryForm.page,
          limit: this.queryForm.list_row,
          type: 0,
        }
        getUserList(params).then((data) => {
          this.userlist = data.data
          this.totalOnetag = data.count
        })
      },
      add() {
        this.dialogFormVisible = true
        this.wintit = '添加'
        this.getUser()
      },
      send(row) {
        let params = {
          id: row.id,
        }
        addAppMasterJoins(params).then((data) => {
          this.$message.success('添加成功！')
          this.getData()
          this.dialogFormVisible = false
        })
      },
      del(row) {
        let params = {
          id: row.id,
        }
        delAppMasterJoins(params).then((data) => {
          this.$message.success('删除成功！')
          this.getData()
          this.getUser()
        })
      },
      edit(row) {
        this.dialogEdit = true
        this.superid = row.id
      },
      save() {
        let params = {
          id: this.superid,
          order: this.sort,
        }
        editAppMasterJoins(params).then((data) => {
          this.$message.success('修改成功！')
          this.getData()
          this.dialogEdit = false
        })
      },
      setSelectRows() {},
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.getData()
      },
      winSizeChange(val) {
        this.queryForm.list_row = val
        this.getUser()
      },
      winCurrentChange(val) {
        this.queryForm.page = val
        this.getUser()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button {
    font-size: 14px;
  }
</style>
